import resources.Constants;

import registnet.dao.GradePeer;
import registnet.dao.Grade;

import registnet.records.source.Form5Source;
import registnet.records.Form5;
import registnet.records.CourseTaken;
import registnet.records.Student;
import registnet.records.source.StudentSource;

Form5 form5;

/* [start] 	initializes gui variables */
String _actions = bundle.getProperty("registnet.module.student_records.macro.form5.form.actions");
String _export = bundle.getProperty("registnet.module.student_records.macro.form5.form.actions.export");
String _print = bundle.getProperty("registnet.module.student_records.macro.form5.form.actions.print");

String _year_semester = bundle.getProperty("registnet.module.student_records.macro.form5.form.year_semester");
String _total_units = bundle.getProperty("registnet.module.student_records.macro.form5.form.total_units");
String _course_code = bundle.getProperty("registnet.module.student_records.macro.form5.form.course_code");
String _course_title = bundle.getProperty("registnet.module.student_records.macro.form5.form.course_title");
String _section = bundle.getProperty("registnet.module.student_records.macro.form5.form.section");
String _units = bundle.getProperty("registnet.module.student_records.macro.form5.form.units");
String _credit = bundle.getProperty("registnet.module.student_records.macro.form5.form.credit");
String _grade = bundle.getProperty("registnet.module.student_records.macro.form5.form.grade");
String _reexam = bundle.getProperty("registnet.module.student_records.macro.form5.form.reexam");
String _remarks = bundle.getProperty("registnet.module.student_records.macro.form5.form.remarks");

Student student = new StudentSource(DAOStudent.getStudentNumber()).getStudent();

String studentNameValue = student.getFirstName() + " " + student.getMiddleName() + " " + student.getLastName();
String studentNumberValue = student.getStudentNumber();
String degreeValue = student.getCurriculum().getDegreeProgram().getDegreeCode();
/* [end] */



/* [start]	initializes the year-semester hashmap */	
List grades = new GradePeer().getGradeByStudent(DAOStudent.getStudentId());
List ascendingKey = new Vector();
HashMap yearSemester = new HashMap();
for(int i=0; i<grades.size(); i++){
	Grade grade = grades.get(i);
	int[] y_s = new int[2];
	y_s[0] = grade.getSemester();
	y_s[1] = grade.getAcademicYear();
	String key;
	if (y_s[1] == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
		key = (y_s[1] - 1) +"-"+ y_s[1] +":"+ Constants.MAP_SEMESTER_VALUE.get(y_s[0]);
	else
		key = y_s[1] +"-"+ (y_s[1] + 1) +":"+ Constants.MAP_SEMESTER_VALUE.get(y_s[0]);
	if (!yearSemester.containsKey(key)){
		yearSemester.put(key, y_s);
		ascendingKey.add(key);
	}
}
/* [end] */



/* [start]	updates the contents of the form5 depending on the year-semester */
void updateForm5(int semester, int year){
	Form5Source form5Source = new Form5Source(student, semester, year);
	form5 = form5Source.getForm5();
	total_units_l.value = form5.getTotalUnits() + "";
	retrieveCourses();
}
/* [end] */



/* [start]	defines a renderer for the contents of the form5 */
private class Form5Renderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		new Label(data[0]).setParent(row);
		new Label(data[1]).setParent(row);
		new Label(data[2]).setParent(row);
		new Label(data[3]).setParent(row);
		new Label(data[4]).setParent(row);
		new Label(data[5]).setParent(row);
		new Label(data[6]).setParent(row);
		new Label(data[7]).setParent(row);
	}
}
/* [end] */



/* [start]	retrieves the courses on the form5 and places them on the grid */
void retrieveCourses(){
	HashMap courses = form5.getCourses();
	Iterator iter = courses.keySet().iterator();
	String[][] listModel = new String[courses.keySet().size()][8];
	int i = 0;
	while(iter.hasNext()){
		CourseTaken course = courses.get(iter.next());
		listModel[i][0] = course.getCourseCode();
		listModel[i][1] = course.getCourseName();
		listModel[i][2] = course.getSection().getSectionName();
		listModel[i][3] = course.getUnit() + "";
		listModel[i][4] = course.getCredit() + "";
		listModel[i][5] = Constants.MAP_GRADE_VALUE.get(course.getGrade());
		if (course.getReExam() == 0)
			listModel[i][6] = "-";
		else
			listModel[i][6] = Constants.MAP_GRADE_VALUE.get(course.getReExam());
		listModel[i][7] = course.getRemarks();
		i++;
	}
	ListModel strset = new SimpleListModel(listModel);
	form5_g.setModel(strset);
	form5_g.setRowRenderer(new Form5Renderer());
}
/* [end] */
